package com.forlove.app.qselect.answer.ui.fragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.forlove.app.R;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.common.widget.recyview.BaseRecyclerAdapter;
import com.forlove.app.manager.FriendManager;
import com.forlove.app.qselect.friend.adapter.SetCandidateAdapter;
import com.forlove.app.qselect.friend.entity.MineFriendEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 设置候选人fragment
 * Created by yjl on 2017/12/5.
 */

public class SetCandidateFragment extends BaseCandidateFragment {
    private TextView text_select_num;
    private RecyclerView recycler_set_candidate;
    private TextView text_cancel;
    private TextView text_sure;
    private RelativeLayout view_no_friend;
    private SetCandidateAdapter setCandidateAdapter;
    private String queryStr;
    private List<MineFriendEntity> copySelectFriend = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.layout_set_anwser_canditator;
    }

    @Override
    protected void initViews() {
        text_select_num = findViewById(R.id.text_select_num);
        recycler_set_candidate = findViewById(R.id.recycler_set_candidate);
        text_cancel = findViewById(R.id.text_cancel);
        text_sure = findViewById(R.id.text_sure);
        view_no_friend = findViewById(R.id.view_no_friend);
        text_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectFriend.size() < 4) {
                    Toast.makeText(getContext(), "至少选择四个好友才可以玩!", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (selectFriendListener != null) {
                    selectFriendListener.selectedFirend(selectFriend, position_set_candidate);
                }
            }
        });
        text_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //还原候选人列表
                for (MineFriendEntity mineFriendEntity : selectFriend) {
                    mineFriendEntity.isRecomend = false;
                }
                for (MineFriendEntity mineFriendEntity : copySelectFriend) {
                    mineFriendEntity.isRecomend = true;
                }
                selectFriend.clear();
                selectFriend.addAll(copySelectFriend);

                if (selectFriendListener != null) {
                    selectFriendListener.closeDialog();
                }
            }
        });
        initRecyclerView();
    }

    private void initRecyclerView() {
        recycler_set_candidate = findViewById(R.id.recycler_set_candidate);
        setCandidateAdapter = new SetCandidateAdapter(getContext(), R.layout.item_add_friends, mineFriendEntities, basePresenter);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
        recycler_set_candidate.setLayoutManager(gridLayoutManager);
        recycler_set_candidate.setAdapter(setCandidateAdapter);
        setCandidateAdapter.hideEmptyView();
        setCandidateAdapter.hideFooterView();
        showEmptyView();
        addClickListener();
        updateView();

    }


    protected synchronized void updateView() {
        if (setCandidateAdapter != null) {
            setCandidateAdapter.updateDatas(mineFriendEntities);
        }
        if (text_select_num != null) {
            text_select_num.setText(selectFriend.size() + "/" + mineFriendEntities.size());
        }
        copySelectFriend.clear();
        copySelectFriend.addAll(selectFriend);
        showEmptyView();
    }

    private void addClickListener() {
        setCandidateAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                MineFriendEntity mineFriendEntity = null;
                if (StringUtils.isNotEmpty(queryStr) && queryFriend.size() > position) {
                    mineFriendEntity = queryFriend.get(position);
                } else if (position < mineFriendEntities.size()) {
                    mineFriendEntity = mineFriendEntities.get(position);
                }
                if (mineFriendEntity != null && mineFriendEntity.isRecomend) {
                    mineFriendEntity.isRecomend = false;
                    selectFriend.remove(mineFriendEntity);
                } else if (mineFriendEntity != null) {
                    mineFriendEntity.isRecomend = true;
                    selectFriend.add(mineFriendEntity);
                }
                setCandidateAdapter.notifyItemChanged(position);
                text_select_num.setText(selectFriend.size() + "/" + mineFriendEntities.size());
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    private void showEmptyView() {
        if (mineFriendEntities.size() == 0 && view_no_friend != null) {
            view_no_friend.setVisibility(View.VISIBLE);
        } else if (view_no_friend != null) {
            view_no_friend.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void inputQuery(String queryStr) {
        this.queryStr = queryStr;
        queryFriend.clear();
        queryFriend.addAll(getLocalSearchFriend(this.queryStr));
        if (StringUtils.isNotEmpty(queryStr) && setCandidateAdapter != null) {
            setCandidateAdapter.setQueryStr(queryStr);
            setCandidateAdapter.updateDatas(queryFriend);
        } else if (setCandidateAdapter != null) {
            List<MineFriendEntity> list = new ArrayList<>();
            list.addAll(mineFriendEntities);
            mineFriendEntities.clear();
            mineFriendEntities.addAll(FriendManager.sortRecommend(list));
            setCandidateAdapter.updateDatas(mineFriendEntities);
        }
    }

    @Override
    public void cancelQuery() {
        this.queryStr = "";
        if (queryFriend != null) {
            queryFriend.clear();
        }
        if (setCandidateAdapter != null) {
            setCandidateAdapter.setQueryStr(queryStr);
        }

    }

    @Override
    public void onPageSelect(int position) {
        if (position == position_set_candidate) {
            cancelQuery();
            updateView();
        }

    }
}
